//
//  SEGShippingAddressTableViewCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2016/12/9.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGShippingAddressTableViewCell.h"

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

@implementation SEGShippingAddressTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = kColor_W;
        [self setUpSubviews];
    }


    return self;
}

- (void)setUpSubviews
{
    //没地址UI 默认情况
    self.selectLabel = [[UILabel alloc] init];
    self.selectLabel.textColor = kColor_1;
    self.selectLabel.font = [UIFont systemFontOfSize:16];
    self.selectLabel.text = @"请选择收货地址";
    [self.contentView addSubview:self.selectLabel];

    self.arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")]];
    self.arrowImg.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:self.arrowImg];

    self.lineImg = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_address_line")]];
    [self.contentView addSubview:self.lineImg];

    [self.lineImg mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.contentView.mas_bottom).priorityHigh();
        make.height.mas_equalTo(3);
    }];
    [self.selectLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(15);
        make.top.mas_equalTo(26);
        make.bottom.equalTo(self.lineImg.mas_top).mas_offset(-26);
    }];
    [self.arrowImg mas_makeConstraints:^(MASConstraintMaker* make) {
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.centerY.equalTo(self.selectLabel.mas_centerY);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(15);
    }];

    //有地址情况UI
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = kColor_1;
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.hidden = YES;

    self.phoneLabel = [[UILabel alloc] init];
    self.phoneLabel.textColor = kColor_1;
    self.phoneLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:self.phoneLabel];
    self.phoneLabel.hidden = YES;

    self.defaultLabel = [[UILabel alloc] init];
    self.defaultLabel.textColor = kColor_T;
    self.defaultLabel.font = [UIFont systemFontOfSize:13];
    self.defaultLabel.text = @" 默认 ";
    self.defaultLabel.layer.cornerRadius = 2;
    self.defaultLabel.layer.masksToBounds = YES;
    self.defaultLabel.layer.borderColor = kColor_T.CGColor;
    self.defaultLabel.layer.borderWidth = 0.5;
    [self.contentView addSubview:self.defaultLabel];
    self.defaultLabel.hidden = YES;

    self.addressImg = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_address_list")]];
    [self.contentView addSubview:self.addressImg];
    self.addressImg.hidden = YES;

    self.addressLabel = [[UILabel alloc] init];
    self.addressLabel.textColor = kColor_1;
    self.addressLabel.font = [UIFont systemFontOfSize:13];
    self.addressLabel.hidden = YES;
    self.addressLabel.numberOfLines = 0;
    [self.contentView addSubview:self.addressLabel];

//    CGSize textSize = [@"最多有五个字" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.top.mas_offset(15);
    }];

    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
    }];

    [self.defaultLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.mas_equalTo(self.phoneLabel.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
    }];

    [self.addressImg sizeToFit];
    [self.addressImg mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.nameLabel.mas_left);
        make.width.mas_equalTo(self.addressImg.bounds.size.width);
        make.height.mas_equalTo(self.addressImg.bounds.size.height);
    }];

    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.addressImg.mas_top);
        make.left.mas_equalTo(self.addressImg.mas_right).mas_offset(5);
        make.right.mas_equalTo(self.arrowImg.mas_left).mas_offset(-13);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
}

- (void)showWithModel:(SEGAddressModel*)addressModel {
    if (!addressModel) {
        return;
    }
    BOOL shoudShowAddress = (addressModel.userAddress.length > 0) ? YES : NO;
    self.selectLabel.hidden = shoudShowAddress;
    self.nameLabel.hidden = !shoudShowAddress;
    self.phoneLabel.hidden = !shoudShowAddress;
    self.addressImg.hidden = !shoudShowAddress;
    self.addressLabel.hidden = !shoudShowAddress;

    if (shoudShowAddress) {
        if(addressModel.contactName.length > 4) {
            self.nameLabel.text = [NSString stringWithFormat:@"%@...",[addressModel.contactName substringToIndex:4]];
        }else {
            self.nameLabel.text = addressModel.contactName;
        }
        self.phoneLabel.text = addressModel.contactPhone;
        self.addressLabel.text = addressModel.userAddress;

        self.defaultLabel.hidden = ![addressModel.defaultType boolValue];
    }
    else {
        self.defaultLabel.hidden = YES;
    }
}

@end
